//
//  SeekSelectViewController.m
//  cityCar
//
//  Created by 刘伟 on 14-11-1.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "SeekSelectViewController.h"
#import "SeekSelectModel.h"
#import "SeekSelectCell.h"
#import "UIImageView+WebCache.h"
#import "SeekWebViewController.h"
#import "MJRefresh.h"
#import "Car.h"

@interface SeekSelectViewController ()

@end

@implementation SeekSelectViewController

- (void)dealloc
{
    [_string release];
    [_arr release];
    [_tableView release];
    [_dictionary release];
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.arr = [NSMutableArray array];
        self.countx = 1;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setupView];
    [self.navigationController setNavigationBarHidden:YES];
    self.tabBarController.tabBar.hidden = YES;

    
    // Do any additional setup after loading the view.
    [self startConnection];
    [self setupRefresh];
}
- (void)setupView
{
    
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
    view1.backgroundColor = [WebColor whiteSmoke];
    [self.view addSubview:view1];
    [view1 release];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 30, 20, 60, 25)];
    label.text = @"详情";
    [view1 addSubview:label];
    
    UIButton *button = [UIButton buttonWithType:(UIButtonTypeSystem)];
    button.frame = CGRectMake(10, 25, 30, 30);
    [button setBackgroundImage:[UIImage imageNamed:@"iconfont-fanhui.png"] forState:(UIControlStateNormal)];
    [button addTarget:self action:@selector(buttonClicked:) forControlEvents:(UIControlEventTouchUpInside)];
    [view1 addSubview:button];
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64) style:(UITableViewStylePlain)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 80;
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[SeekSelectCell class] forCellReuseIdentifier:@"reuse"];
    [label      release];
    [_tableView release];
}
- (void)buttonClicked:(UIButton *)button
{
    [self.navigationController setNavigationBarHidden:NO];
    self.tabBarController.tabBar.hidden = NO;
    [self.navigationController popViewControllerAnimated:YES];
//        [self dismissViewControllerAnimated:YES completion:^{
//    
//        }];

}
- (void)startConnection
{
    
    NSMutableString *str1 = [NSMutableString stringWithFormat:@"http://mrobot.pcauto.com.cn/v2/price/models/search?price=%@&jb=%@&pl%@&bsx=%@&pageNo=%d&pageSize=20&v=4.3.0", [self.dictionary objectForKey:@"jiage"], [self.dictionary objectForKey:@"jibie"], [self.dictionary objectForKey:@"pailiang"], [self.dictionary objectForKey:@"biansuxiang"], self.countx];
    NSLog(@"=========%@", str1);
    [Connection networkHandlerWith:str1 block:^(id result) {
        NSMutableArray *array = [result objectForKey:@"data"];
//        self.arr = [NSMutableArray array];
        for (NSMutableDictionary *dic in array) {
            SeekSelectModel *model = [[SeekSelectModel alloc] init];
            model.photo = [dic objectForKey:@"photo"];
            model.priceRange = [dic objectForKey:@"priceRange"];
            model.serialName = [dic objectForKey:@"serialName"];
            model.seriaId    = [dic objectForKey:@"serialId"];
            [self.arr addObject:model];
            [model release];
        }
        [self.tableView reloadData];
    }];
    
    // 2.2秒后刷新表格UI
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        [self.tableView reloadData];
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [self.tableView footerEndRefreshing];
        _countx += 1;
        NSLog(@"self.countx %d", self.countx);
    });

    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SeekSelectCell *cell1 = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    SeekSelectModel *model = self.arr[indexPath.row];
    [cell1.MyImageView sd_setImageWithURL:[NSURL URLWithString:model.photo] placeholderImage:[UIImage imageNamed:@"NoPicture.png"]];
    cell1.priceLabel.text = model.priceRange;
    cell1.priceLabel.textColor = [WebColor red];
    cell1.priceLabel.font = [UIFont systemFontOfSize:15];
    cell1.serialLabel.text = model.serialName;
    return cell1;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    SeekWebViewController *web = [[SeekWebViewController alloc] init];
    SeekSelectModel *model = self.arr[indexPath.row];
    NSMutableString *string = [NSMutableString stringWithFormat:@"http://price.pcauto.com.cn/sg%@/", model.seriaId];
    web.car.Id = model.seriaId;
    web.car.logo = model.photo;
    web.car.name = model.serialName;
    web.Idurl = string;
    
    [self.navigationController pushViewController:web animated:YES];
    [web release];
}
- (void)setupRefresh
{
    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
//    [self.tableView addHeaderWithTarget:self action:@selector(headerRereshing)];
//    [self.tableView headerBeginRefreshing];
    
    // 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
    [self.tableView addFooterWithTarget:self action:@selector(startConnection)];
    [self.tableView footerEndRefreshing];
    
    //    // 设置文字(也可以不设置,默认的文字在MJRefreshConst中修改)
    //    self.leftTable.headerPullToRefreshText = @"下拉以上弦...";
    //    self.leftTable.headerReleaseToRefreshText = @"松开即可上弦...";
    //    self.leftTable.headerRefreshingText = @"上弦中...";
    //
    //    self.leftTable.footerPullToRefreshText = @"上拉可以加载更多数据了";
    //    self.leftTable.footerReleaseToRefreshText = @"松开马上加载更多数据了";
    //    self.leftTable.footerRefreshingText = @"加载中";
    
//    self.countx = 2;
//    NSLog(@"se.countx %ld", self.countx);
}
#pragma mark 开始进入刷新状态
//- (void)headerRereshing
//{
//    // 2.2秒后刷新表格UI
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        
//        // 刷新表格
//        [self.leftTable reloadData];
//        //        [_tableView reloadData];
//        
//        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
//        [self.leftTable  headerEndRefreshing];
//    });
//}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
